import { randInt } from "../random"

export function shuffle(arr: any[]): any[] {
  if (arr == null) return []
  const results: any[] = []

  let i = -1
  const len = arr.length
  while (++i < len) {
    if (!i) {
      results[0] = arr[0]
    } else {
      const rnd = randInt(0, i)
      results[i] = results[rnd]
      results[rnd] = arr[i]
    }
  }

  return results
}
